# -*- coding: utf-8 -*-

from abstract_sensor.sensor_interfaces import SimulatedSensor
from abstract_sensor.definition import SmartAbstractSensor

import quantities
import signal
import time
import sys
import random

def sig_handler(signum, frame):
    print "\nAus Maus"    
    sys.exit(1)

class MyAbstractSensorNode(SmartAbstractSensor):
    
    def __init__(self, outputDataSheet, sensor_interface, appName, outputLayer=[], logger=[]):
        SmartAbstractSensor.__init__(self, 
                                     outputDataSheet=outputDataSheet,
                                     sensor_interface=sensor_interface,
                                     appName = appName)
        
    def input_event_mapping(self, value, time=[]):
        '''
        The user have to map the incoming sensor measurements to the input event format
        Here MOSAIC ensures that only valid information (correct size, unit, etc.) are
        integrated.
        '''
        self.input_layer.input_event.set_value(value*quantities.meter, 'Measurement')
        self.input_layer.input_event.set_value([0.5]*quantities.centimeter**2, 'Uncertainty') 
      
    def input_event_filtering(self):
        '''
        This function works as a first filter - only events that pass this function are
        storaged for further processing.
        If an input event is transfered to the input_events - list the function outputs
        a True
        '''
        self.input_layer.input_events.append(self.input_layer.input_event)
        return True        
        
    def input_event_access(self):       
        '''
        The access of the processing functions to the current available events is 
        prepared here. Additional filters can be implemented to obtain the newest
        event, a number of 10 events etc
        '''
        self.input_events=self.input_layer.input_events
        print "Input events", self.input_events
    
    def processing_ctr(self):
        '''
        Process control manages the fusion, filtering, estimation etc. algorithms and
        calculates a individual validity value for each step
        '''
        results=[]
        return results
    
    def evaluation_fct(self):
        '''
        The evaluation_fct calculates a commmon validity for the output event using
        the individual validity output of each processing function.
        '''
        common_validity=1
        return common_validity
    
    def output_event_mapping(self, results, common_validity):
        meas=self.input_events[0].get_value('Measurement')
        self.output_event.set_value(meas, 'Measurement')
        self.output_event.set_value(5.0*quantities.second, 'Timestamp')
        self.output_event.set_value([0.5]*quantities.centimeter**2, 'Uncertainty')
        self.output_event.set_value([100, 200, 0]*quantities.centimeter, 'Position')
        self.output_event.set_value([100, 200, 0]*quantities.degree, 'Orientation') 
        self.output_event.set_value([common_validity], 'Validity')        


if __name__ == "__main__":

    sensor_file = '../datasheets/sensors/' + 'IRDistanceSensor.xml'
    simulatedSensorInterf = SimulatedSensor(sensorDataSheet=sensor_file,
                                               position=[],
                                               orientation=[]) 

    channel_file='../datasheets/events/'+"Dist_IRe.xml"
    SAS = MyAbstractSensorNode(outputDataSheet=channel_file,
                                 sensor_interface=simulatedSensorInterf,
                                 appName="SAS Test",
                                 logger=[])
    print SAS       
    SAS.startMeasurements()
    
    print "Mit STRG + C beenden."
    signal.signal(signal.SIGINT, sig_handler)    
    
    while 1:
        time.sleep(1)
